﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.Common.DataLayer;
using ICeFramework.Common.DataLayer.Interfaces;

namespace ICeFramework.Business.DataLayer
{
    public class EntityObject<TEntity> : IEntityObject
        where TEntity : class
    {
        public event EntityObjectValidationDelegate ValidationError;

        private string _errorMessage;
        #region public string Error
        /// <summary>
        /// Get/Sets the Error of the EntityObject
        /// </summary>
        /// <value></value>
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }
        #endregion

        private string _errorCode;
        #region public string ErrorCode
        /// <summary>
        /// Get/Sets the ErrorCode of the EntityObject
        /// </summary>
        /// <value></value>
        public string ErrorCode
        {
            get { return _errorCode; }
            set { _errorCode = value; }
        }
        #endregion

        #region public object Value
        /// <summary>
        /// Get/Sets the Value of the EntityObject
        /// </summary>
        /// <value></value>
        public object Value
        {
            get
            {
                object val = _row.DataRow[_column.Name];
                if (val == DBNull.Value)
                {
                    val = null;
                }
                return val;
            }
            set
            {
                object val = value;
                if (val == null || (value != null && string.IsNullOrEmpty(val.ToString())))
                {
                    val = DBNull.Value;
                }
                _isValid = Validate(val);
                if (_isValid)
                {
                    _row.DataRow[_column.Name] = val;
                    _errorMessage = string.Empty;
                    _errorCode = string.Empty;
                }
            }
        }
        #endregion

        private IEntityRow _row;
        #region public IEntityRow Row
        /// <summary>
        /// Gets the Row of the EntityObject
        /// </summary>
        /// <value></value>
        public IEntityRow Row
        {
            get { return _row; }
        }
        #endregion

        private EntityColumn _column;
        #region public EntityColumn Column
        /// <summary>
        /// Get/Sets the Column of the EntityObject
        /// </summary>
        /// <value></value>
        public EntityColumn Column
        {
            get { return _column; }
            internal set { _column = value; }
        }
        #endregion

        private bool _isValid = true;
        #region public virtual bool IsValid
        /// <summary>
        /// Gets the IsValid of the EntityObject
        /// </summary>
        /// <value></value>
        public virtual bool IsValid
        {
            get { return _isValid; }
        }
        #endregion

        #region public EntityObject(IEntityRow row, EntityColumn column)
        /// <summary>
        /// Initializes a new instance of the <b>EntityObject&lt;TEntity&gt;</b> class.
        /// </summary>
        /// <param name="row"></param>
        /// <param name="column"></param>
        public EntityObject(IEntityRow row, EntityColumn column)
        {
            _row = row;
            _column = column;
            _isValid = Validate(Value);
        }
        #endregion

        #region public virtual bool Validate(object value)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public virtual bool Validate(object value)
        {
            _errorMessage = string.Empty;
            _errorCode = string.Empty;
            if ((_column.Validation & ValidationFlags.Mandatory) == ValidationFlags.Mandatory && (value == null || value == DBNull.Value))
            {
                _errorMessage = string.Format("{0} is required.", _column.Name);
                _errorCode = "0";
                OnValidationError(new EntityObjectValidationArgs(_errorCode, _errorMessage, this));
                return false;
            }

            if (!ValidationHelper.ValidateValue(value, _column.ColumnType, ((_column.Validation & ValidationFlags.Mandatory) == ValidationFlags.Mandatory)))
            {
                _errorMessage = ValidationHelper.GetErrorMessage(ValidationType.Conversion, _column);
                _errorCode = "1";
                OnValidationError(new EntityObjectValidationArgs(_errorCode, _errorMessage, this));
                return false;
            }
            return true;
        }
        #endregion

        #region public virtual void OnValidationError(EntityObjectValidationArgs args)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        public virtual void OnValidationError(EntityObjectValidationArgs args)
        {
            if (ValidationError != null)
            {
                ValidationError(this, args);
            }
        }
        #endregion

        #region IEntityColumn IEntityObject.Column
        /// <summary>
        /// Gets the IEntityObject.Column of the EntityObject
        /// </summary>
        /// <value></value>
        IEntityColumn IEntityObject.Column
        {
            get { return _column as IEntityColumn; }
        }
        #endregion
    }
}
